﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

class BufferedConsole : IDisposable
{
    const int Buff_Size = 25;

    private StringBuilder console;

    ~BufferedConsole()
    {
        Dispose(false);
    }

    public void Dispose()
    {
        Dispose(true);
        GC.SuppressFinalize(this);
    }

    private void Dispose(bool disposing)
    {
        if (this.console.Length > 0)
        {
            if (disposing)
            {
                // Free any managed resources here
            }

            Console.WriteLine(this.console.ToString());
            Console.WriteLine("Buffer cleared by dispose.");
            this.console.Clear();            
        }
    }

    public BufferedConsole()
    {        
        this.console = new StringBuilder(Buff_Size);
    }

    public void Write(string message)
    {
        if ((message.Length + this.console.Length) > Buff_Size)
        {
            this.console.Append(message);
            Console.WriteLine(this.console.ToString());
            Console.WriteLine("Buff cleared by me.");
            this.console.Clear();
        }
        else
        {
            this.console.Append(message);
        }
    }
}
